package cn.com.qiter.staservice.service.impl;

import cn.com.qiter.common.AppConstant;
import cn.com.qiter.common.Result;
import cn.com.qiter.common.cms.CmsDictionaryModel;
import cn.com.qiter.common.log.OperConstant;
import cn.com.qiter.common.ordervo.UcenterMemberOrder;
import cn.com.qiter.common.vo.ProjectModel;
import cn.com.qiter.staservice.client.CmsClient;
import cn.com.qiter.staservice.client.TrafficClient;
import cn.com.qiter.staservice.client.UcenterClient;
import cn.com.qiter.staservice.entity.StatisticsUserdaily;
import cn.com.qiter.staservice.entity.StatisticsWorkdaily;
import cn.com.qiter.staservice.mapper.StatisticsUserdailyMapper;
import cn.com.qiter.staservice.service.StatisticsUserdailyService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

/**
 * <p>
 * 统计日数据 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2021-05-13
 */
@Service
public class StatisticsUserdailyServiceImpl extends ServiceImpl<StatisticsUserdailyMapper, StatisticsUserdaily> implements StatisticsUserdailyService {

    @Autowired
    private UcenterClient ucenterClient;
    @Autowired
    CmsClient cmsClient;
    @Autowired
    private TrafficClient trafficClient;




    /**
     * 针对定时任务调度
     * @param day
     */
    @Override
    public void countTrap(String day) {

        Result dict = cmsClient.get("8");
        LinkedHashMap dichash = (LinkedHashMap) dict.getData().get("item");
        String value = (String) dichash.get("value");
        //远程调用得到某一天注册人数
        Result result = ucenterClient.findAllMember();
        ArrayList<LinkedHashMap> memberOrders = (ArrayList) result.getData().get("items");
        //远程调用得到某一天注册人数
        Result projectAll = ucenterClient.getProjectAll();
        ArrayList<LinkedHashMap> projectModels = (ArrayList) projectAll.getData().get("items");
//
//        Result workTYpesResult = cmsClient.getDictByName("work.type");
//        ArrayList<LinkedHashMap> dictionaryModels = (ArrayList) workTYpesResult.getData().get("items");
//

        for (LinkedHashMap memberOrder:
                memberOrders) {
            String memberId = (String) memberOrder.get("id");
            for (LinkedHashMap projectModel:
                    projectModels) {
                String projectIds = (String) projectModel.get("id");
                QueryWrapper<StatisticsUserdaily> wrapper = new QueryWrapper<>();
                wrapper.eq("project_id",projectIds);
                wrapper.eq("user_id",memberId);
                wrapper.eq("date_calculated",day);
                wrapper.eq("work_type", value);
                baseMapper.delete(wrapper);

                //远程调用得到某一天注册人数
                Result countTrapDayByProjectAndMember = ucenterClient.countTrapDay(day,projectIds,memberId);
                Integer ctdpm = (Integer) countTrapDayByProjectAndMember.getData().get("countDay");

                //把获取数据添加数据库，统计分析表里面
                StatisticsUserdaily sta = new StatisticsUserdaily();
                sta.setWorkNum(ctdpm); //数量
                sta.setDateCalculated(day);//统计日期
                sta.setUserId(memberId);
                sta.setNickname((String) memberOrder.get("nickname"));
                sta.setProjectId(projectIds);
                sta.setWorkType(value);
                baseMapper.insert(sta);
            }
        }


    }

    private void count(String day, String value) {
        //远程调用得到某一天注册人数
        Result result = ucenterClient.findAllMember();
        ArrayList<LinkedHashMap> memberOrders = (ArrayList) result.getData().get("items");
        //远程调用得到某一天注册人数
        Result projectAll = ucenterClient.getProjectAll();
        ArrayList<LinkedHashMap> projectModels = (ArrayList) projectAll.getData().get("items");
//
//        Result workTYpesResult = cmsClient.getDictByName("work.type");
//        ArrayList<LinkedHashMap> dictionaryModels = (ArrayList) workTYpesResult.getData().get("items");
//

        for (LinkedHashMap memberOrder:
                memberOrders) {
            String memberId = (String) memberOrder.get("id");
            for (LinkedHashMap projectModel:
                    projectModels) {
                String projectIds = (String) projectModel.get("id");
                QueryWrapper<StatisticsUserdaily> wrapper = new QueryWrapper<>();
                wrapper.eq("project_id",projectIds);
                wrapper.eq("user_id",memberId);
                wrapper.eq("date_calculated",day);
                wrapper.eq("work_type", value);
                baseMapper.delete(wrapper);

                //远程调用得到某一天注册人数
                Result countTrapDayByProjectAndMember = ucenterClient.countTrapDay(day,projectIds,memberId);
                Integer ctdpm = (Integer) countTrapDayByProjectAndMember.getData().get("countDay");

                //把获取数据添加数据库，统计分析表里面
                StatisticsUserdaily sta = new StatisticsUserdaily();
                sta.setWorkNum(ctdpm); //数量
                sta.setDateCalculated(day);//统计日期
                sta.setUserId(memberId);
                sta.setNickname((String) memberOrder.get("nickname"));
                sta.setProjectId(projectIds);
                sta.setWorkType(value);
                baseMapper.insert(sta);
            }
        }

    }

    /**
     * 用户个人数据统计分析
     * 每天，每个项目，每个用户
     *
     * @param day
     */
    @Override
    public void countPests(String day) {

        Result dict = cmsClient.get("7");
        LinkedHashMap dichash = (LinkedHashMap) dict.getData().get("item");
        String value = (String) dichash.get("value");
        //远程调用得到某一天注册人数
        Result result = ucenterClient.findAllMember();
        ArrayList<LinkedHashMap> memberOrders = (ArrayList) result.getData().get("items");
        //远程调用得到某一天注册人数
        Result projectAll = ucenterClient.getProjectAll();
        ArrayList<LinkedHashMap> projectModels = (ArrayList) projectAll.getData().get("items");
//
//        Result workTYpesResult = cmsClient.getDictByName("work.type");
//        ArrayList<LinkedHashMap> dictionaryModels = (ArrayList) workTYpesResult.getData().get("items");
//

        for (LinkedHashMap memberOrder:
                memberOrders) {
            String memberId = (String) memberOrder.get("id");
            for (LinkedHashMap projectModel:
                    projectModels) {
                String projectIds = (String) projectModel.get("id");
                QueryWrapper<StatisticsUserdaily> wrapper = new QueryWrapper<>();
                wrapper.eq("project_id",projectIds);
                wrapper.eq("user_id",memberId);
                wrapper.eq("date_calculated",day);
                wrapper.eq("work_type", value);
                baseMapper.delete(wrapper);

                //远程调用得到某一天注册人数
                Result countTrapDayByProjectAndMember = ucenterClient.countPestsDay(day,projectIds,memberId);
                Integer ctdpm = (Integer) countTrapDayByProjectAndMember.getData().get("countDay");

                //把获取数据添加数据库，统计分析表里面
                StatisticsUserdaily sta = new StatisticsUserdaily();
                sta.setWorkNum(ctdpm); //数量
                sta.setDateCalculated(day);//统计日期
                sta.setUserId(memberId);
                sta.setNickname((String) memberOrder.get("nickname"));
                sta.setProjectId(projectIds);
                sta.setWorkType(value);
                baseMapper.insert(sta);
            }
        }


    }

    /**
     * 用户个人数据统计分析
     * 每天，每个项目，每个用户
     *
     * @param day
     */
    @Override
    public void countOther(String day) {

        Result dict = cmsClient.get("6");
        LinkedHashMap dichash = (LinkedHashMap) dict.getData().get("item");
        String value = (String) dichash.get("value");
        //远程调用得到某一天注册人数
        Result result = ucenterClient.findAllMember();
        ArrayList<LinkedHashMap> memberOrders = (ArrayList) result.getData().get("items");
        //远程调用得到某一天注册人数
        Result projectAll = ucenterClient.getProjectAll();
        ArrayList<LinkedHashMap> projectModels = (ArrayList) projectAll.getData().get("items");
//
//        Result workTYpesResult = cmsClient.getDictByName("work.type");
//        ArrayList<LinkedHashMap> dictionaryModels = (ArrayList) workTYpesResult.getData().get("items");
//

        for (LinkedHashMap memberOrder:
                memberOrders) {
            String memberId = (String) memberOrder.get("id");
            for (LinkedHashMap projectModel:
                    projectModels) {
                String projectIds = (String) projectModel.get("id");
                QueryWrapper<StatisticsUserdaily> wrapper = new QueryWrapper<>();
                wrapper.eq("project_id",projectIds);
                wrapper.eq("user_id",memberId);
                wrapper.eq("date_calculated",day);
                wrapper.eq("work_type", value);
                baseMapper.delete(wrapper);

                //远程调用得到某一天注册人数
                Result countTrapDayByProjectAndMember = trafficClient.countOtherDay(day,projectIds,memberId);
                Integer ctdpm = (Integer) countTrapDayByProjectAndMember.getData().get("countDay");

                //把获取数据添加数据库，统计分析表里面
                StatisticsUserdaily sta = new StatisticsUserdaily();
                sta.setWorkNum(ctdpm); //数量
                sta.setDateCalculated(day);//统计日期
                sta.setUserId(memberId);
                sta.setNickname((String) memberOrder.get("nickname"));
                sta.setProjectId(projectIds);
                sta.setWorkType(value);
                baseMapper.insert(sta);
            }
        }


    }

    /**
     * 用户个人数据统计分析
     * 每天，每个项目，每个用户
     *
     * @param day
     */
    @Override
    public void countVideo(String day) {

        Result dict = cmsClient.get("5");
        LinkedHashMap dichash = (LinkedHashMap) dict.getData().get("item");
        String value = (String) dichash.get("value");
        //远程调用得到某一天注册人数
        Result result = ucenterClient.findAllMember();
        ArrayList<LinkedHashMap> memberOrders = (ArrayList) result.getData().get("items");
        //远程调用得到某一天注册人数
        Result projectAll = ucenterClient.getProjectAll();
        ArrayList<LinkedHashMap> projectModels = (ArrayList) projectAll.getData().get("items");
//
//        Result workTYpesResult = cmsClient.getDictByName("work.type");
//        ArrayList<LinkedHashMap> dictionaryModels = (ArrayList) workTYpesResult.getData().get("items");
//

        for (LinkedHashMap memberOrder:
                memberOrders) {
            String memberId = (String) memberOrder.get("id");
            for (LinkedHashMap projectModel:
                    projectModels) {
                String projectIds = (String) projectModel.get("id");
                QueryWrapper<StatisticsUserdaily> wrapper = new QueryWrapper<>();
                wrapper.eq("project_id",projectIds);
                wrapper.eq("user_id",memberId);
                wrapper.eq("date_calculated",day);
                wrapper.eq("work_type", value);
                baseMapper.delete(wrapper);

                //远程调用得到某一天注册人数
                Result countTrapDayByProjectAndMember = trafficClient.countVideoDay(day,projectIds,memberId);
                Integer ctdpm = (Integer) countTrapDayByProjectAndMember.getData().get("countDay");

                //把获取数据添加数据库，统计分析表里面
                StatisticsUserdaily sta = new StatisticsUserdaily();
                sta.setWorkNum(ctdpm); //数量
                sta.setDateCalculated(day);//统计日期
                sta.setUserId(memberId);
                sta.setNickname((String) memberOrder.get("nickname"));
                sta.setProjectId(projectIds);
                sta.setWorkType(value);
                baseMapper.insert(sta);
            }
        }



    }

    @Override
    public Result getShowData(String projectId,String userId, String type, String begin, String end){
        //根据条件查询对应数据
        QueryWrapper<StatisticsUserdaily> wrapper = new QueryWrapper<>();
        wrapper.between("date_calculated",begin,end);
        wrapper.eq("work_type",type);
        wrapper.eq("project_id",projectId);
        wrapper.eq("user_id",userId);
        List<StatisticsUserdaily> staList = this.list(wrapper);
        return Result.ok().data("data",staList);
    }


}
